let request = require('request');

let post = function(url, token, data, whenDone, whenError){
    var options = {
        'method': 'POST',
        'url': url,
        'headers': {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        form: data
    };
    if(token){
        options.headers.Authorization = `Bearer ${token}`;
    }

    request(options, function (error, response) { 
        if (error) {
            whenError(error);
        }
        else{
            whenDone(response.body);
        }
    });
}

let get = function(url, token, whenDone, whenError){
    var options = {
        'method': 'GET',
        'url': url,
        'headers': {
          'Authorization': `Bearer ${token}`
        }
    };
    request(options, function (error, response) { 
        if (error) {
            whenError(error);
        }
        else{
            whenDone(response.body);
        }
    });
}

exports.post = post;
exports.get = get;